package leetcode.f1t100;

import leetcode.struct.ListNode;

/**
 * 使用链表计算两数相加, 以链表形式返回
 *
 * https://leetcode.com/problems/add-two-numbers/
 *
 * @author lichx
 * @date 2021/9/14 16:55
 */
public class Q2_AddTwoNumbers {

    public ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        ListNode head = new ListNode((l1.val + l2.val) % 10);
        int digit = (l1.val + l2.val) / 10;
        ListNode cur1 = l1.next, cur2 = l2.next, cur3 = head;
        while (cur1 != null && cur2 != null) {
            cur3.next = new ListNode();
            cur3 = cur3.next;
            cur3.val = (cur1.val + cur2.val + digit) % 10;
            digit = (cur1.val + cur2.val + digit) / 10;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        if (cur1 == null && cur2 == null) {
            cur3.next = digit > 0 ? new ListNode(digit) : null;
            return head;
        }
        ListNode notNull = cur1 == null ? cur2 : cur1;
        while (notNull != null) {
            cur3.next = new ListNode();
            cur3 = cur3.next;
            cur3.val = (notNull.val + digit) % 10;
            digit = (notNull.val + digit) / 10;
            notNull = notNull.next;
        }
        cur3.next = digit > 0 ? new ListNode(digit) : null;
        return head;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode root = null;
        ListNode cur = null;
        int digit=0;
        while(l1 != null || l2!= null){
            int a = 0;
            int b = 0;
            int c = 0;
            int sum = 0;
            if(l1!=null){
                a = l1.val;
                l1 = l1.next;
            }
            if(l2!= null){
                b = l2.val;
                l2 = l2.next;
            }
            sum = a+b+digit;
            c = sum % 10;
            digit = sum /10;

            if(cur==null){
                cur = new ListNode(c);
                root = cur;
            }else{
                cur.next = new ListNode(c);
                cur = cur.next;
            }
        }
        cur.next = digit > 0 ? new ListNode(digit) : null;
        return root;
    }
    public ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode l3 = null;
        ListNode root = null;
        while(l1 != null || l2 != null){
            int a = 0;
            int b = 0;
            int c = 0;
            int sum = 0;
            if(l1 != null){
                a = l1.val;
                l1 = l1.next;
            }
            if(l2 != null){
                b = l2.val;
                l2 = l2.next;
            }
            sum = (a + b + carry) ;
            c = sum % 10;
            carry =  sum/10;

            if (l3 == null) {
                l3 = new ListNode(c);
                root = l3;
            } else {
                l3.next = new ListNode(c);
                l3 = l3.next;
            }

        }
        if (carry != 0 ) {
            l3.next = new ListNode(carry);
        }
        return root;
    }
}
